package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.TInstitution;
import com.ruoyi.system.domain.TInstitutionAudit;
import com.ruoyi.system.domain.TInstitutionPersonDevice;
import com.ruoyi.system.dto.req.AuthenticationReqDto;
import com.ruoyi.system.dto.req.InstitutionReqDto;
import com.ruoyi.system.dto.resq.DeviceRespDto;
import com.ruoyi.system.dto.resq.TInstitutionAuditRespDto;
import com.ruoyi.system.dto.resq.TInstitutionRespDto;
import com.ruoyi.system.mapper.TDeviceMapper;
import com.ruoyi.system.mapper.TInstitutionAuditMapper;
import com.ruoyi.system.mapper.TInstitutionPersonDeviceMapper;
import com.ruoyi.system.service.TInstitutionService;
import com.ruoyi.system.mapper.TInstitutionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author dell
* @description 针对表【t_institution(机构信息表)】的数据库操作Service实现
* @createDate 2023-11-16 15:28:47
*/
@Service
public class TInstitutionServiceImpl extends ServiceImpl<TInstitutionMapper, TInstitution>
    implements TInstitutionService{

    @Autowired
    private TInstitutionMapper tInstitutionMapper;

    @Autowired
    private TInstitutionAuditMapper tInstitutionAuditMapper;

    @Autowired
    private TDeviceMapper tDeviceMapper;

    @Override
    public List<TInstitutionRespDto> selectInstitutionList(InstitutionReqDto institutionReqDto) {

        return tInstitutionMapper.selectInstitutionList(institutionReqDto);
    }

    @Override
    public TInstitutionRespDto selectOneByInstitutionId(String institutionId) {

        return tInstitutionMapper.selectOneByInstitutionId(institutionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editAuthentication(AuthenticationReqDto authenticationReqDto) {

        TInstitution tInstitution = tInstitutionMapper.selectById(authenticationReqDto.getInstitutionId());
        tInstitution.setAuthenticationStatus(authenticationReqDto.getAuthenticationStatus());
        tInstitution.setAuthenticationExplain(authenticationReqDto.getAuthenticationExplain());

        tInstitutionMapper.updateById(tInstitution);

        TInstitutionAudit tInstitutionAudit = new TInstitutionAudit().setInstitutionId(authenticationReqDto.getInstitutionId()).setAuthenticationExplain(authenticationReqDto.getAuthenticationExplain()).setAuthenticationStatus(authenticationReqDto.getAuthenticationStatus())
                .setCreateBy(SecurityUtils.getUsername()).setCreateTime(DateUtils.getNowDate())
                .setUpdateBy(SecurityUtils.getUsername()).setUpdateTime(DateUtils.getNowDate());

        tInstitutionAuditMapper.insert(tInstitutionAudit);

        return AjaxResult.success("确认成功");
    }

    @Override
    public List<TInstitutionAuditRespDto> getInstitutionAuditList(String institutionId) {

        return tInstitutionAuditMapper.selectListByInstitutionId(institutionId);
    }

    @Override
    public List<DeviceRespDto> getDeviceListByInstitutionId(String institutionId) {

//        List<TInstitutionPersonDevice> tInstitutionPersonDevices = tInstitutionPersonDeviceMapper.selectList(new LambdaQueryWrapper<TInstitutionPersonDevice>().eq(TInstitutionPersonDevice::getOwnerId, institutionId));
//
//        List<DeviceRespDto> list = new ArrayList<>();
//
//        for (TInstitutionPersonDevice tInstitutionPersonDevice : tInstitutionPersonDevices) {
//            DeviceRespDto deviceRespDto = tDeviceMapper.selectOneByDeviceId(tInstitutionPersonDevice.getDeviceId());
//            list.add(deviceRespDto);
//        }


        return tDeviceMapper.selectListByOwnerId(institutionId);
    }

}




